package algorithm.sort;

import java.io.*;

/**
 * <p>
 * 利用数组下标排序
 * </p>
 *
 * @author liyi
 * @since 2024/4/18 10:26
 */
public class CountSort {
    public static void main(String[] args) throws Exception {
        String str = null;
        String fileName = "C:\\Users\\liyi\\javaproject\\algorithm\\resource\\08排序2\\200w.txt";
        InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        int data[] = new int[2100002];
        int i = 0;
        while ((str = br.readLine()) != null) {
            double a = Double.valueOf(str);
            a = a * 100;
            data[i++] = (int) a;
            // System.out.println((int) a);
        }
        System.out.println("size=" + i);
        long start = System.currentTimeMillis();
        countSort(data, 0, data.length - 1);
        System.out.println("time: " + (System.currentTimeMillis() - start) + "ms");
    }

    public static void countSort(int data[], int min, int max) throws Exception {
        int counts[] = new int[max + 1];
        for (int i = 0; i < data.length; i++) {
            counts[data[i]]++;
        }

        File file = new File("C:\\Users\\liyi\\javaproject\\algorithm\\resource\\08排序2\\200w-csort.txt");
        Writer out = new FileWriter(file);

        for (int i = 0; i <= max; i++) {
            if (counts[i] > 0) {
                for (int j = 0; j < counts[i]; j++) {
                    out.write(((double) (i / 100.0)) + "\r\n");
                }
            }
        }
        out.close();
    }
}
